from sqlalchemy import Column, Integer, String, DateTime, Boolean, Text
from sqlalchemy.ext.declarative import declarative_base
from datetime import datetime

Base = declarative_base()

# 用户模型（新增）
class User(Base):
    __tablename__ = "users"
    id = Column(Integer, primary_key=True, index=True)
    username = Column(String(50), unique=True, index=True, nullable=False)
    hashed_password = Column(String(255), nullable=False)
    is_active = Column(Boolean, default=True)
    is_admin = Column(Boolean, default=False)
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, onupdate=datetime.utcnow)

    # 添加序列化方法
    def to_dict(self):
        return {
            "id": self.id,
            "username": self.username,
            "is_active": self.is_active,
            "is_admin": self.is_admin,
            "created_at": self.created_at.isoformat() if self.created_at else None,
            "updated_at": self.updated_at.isoformat() if self.updated_at else None
        }

# 提醒模型
class Alert(Base):
    __tablename__ = "alerts"
    id = Column(Integer, primary_key=True, index=True)
    title = Column(String(100), nullable=False)  # 提醒标题
    content = Column(Text, nullable=False)       # 提醒内容
    alert_time = Column(DateTime, nullable=False)# 提醒时间
    repeat = Column(String(50), default="none")  # 重复规则（none/daily/weekly）
    is_active = Column(Boolean, default=True)    # 是否有效
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, onupdate=datetime.utcnow)

    # 添加序列化方法 - 关键修复
    def to_dict(self):
        """将Alert模型对象转换为可JSON序列化的字典"""
        return {
            "id": self.id,
            "title": self.title,
            "content": self.content,
            "alert_time": self.alert_time.isoformat() if self.alert_time else None,
            "repeat": self.repeat,
            "is_active": self.is_active,
            "created_at": self.created_at.isoformat() if self.created_at else None,
            "updated_at": self.updated_at.isoformat() if self.updated_at else None
        }

# 客户端模型（记录心跳状态）
class Client(Base):
    __tablename__ = "clients"
    id = Column(Integer, primary_key=True, index=True)
    client_id = Column(String(50), unique=True, index=True)  # 客户端唯一标识
    client_name = Column(String(100))  # 客户端名称（如"MacBook-Pro"）
    client_type = Column(String(20))   # 客户端类型（windows/mac/android）
    last_heartbeat = Column(DateTime)  # 最后心跳时间
    is_online = Column(Boolean, default=False)  # 是否在线
    created_at = Column(DateTime, default=datetime.utcnow)

    # 添加序列化方法
    def to_dict(self):
        return {
            "id": self.id,
            "client_id": self.client_id,
            "client_name": self.client_name,
            "client_type": self.client_type,
            "last_heartbeat": self.last_heartbeat.isoformat() if self.last_heartbeat else None,
            "is_online": self.is_online,
            "created_at": self.created_at.isoformat() if self.created_at else None
        }